package com.xyp.mesadmin.sys.controller;

import com.xyp.mesadmin.annotation.MyLog;
import com.xyp.mesadmin.sys.param.LoginParam;
import com.xyp.mesadmin.sys.service.SysUserService;
import com.xyp.mescommon.common.R;
import com.xyp.mescommon.entity.sys.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.UUID;

/**
 * --- 加油，努力 ---
 *
 * @Author XYP
 * @Date 2024/9/23
 * @Solgne 从想做到能做
 * @Desc
 */
@RestController
@RequestMapping("/sys/user")
public class LoginController {
    @Autowired
    private SysUserService sysUserService;

    //@MyLog("登录")
    @PostMapping("/login")
    public R login (@RequestBody LoginParam loginParam, HttpSession session) {
        SysUser sysUser = sysUserService.login(loginParam.getUsername(), loginParam.getPassword());
        if (sysUser!=null){
            session.setAttribute("userInfo",sysUser);
            HashMap<String, String> map = new HashMap<>();
            map.put("token", UUID.randomUUID().toString());
            return R.ok(map);
        }
        return R.fail();
    }

    @GetMapping("/info")
    public R getUserInfo(HttpSession session){
        SysUser info = (SysUser) session.getAttribute("userInfo");
        return R.ok(info);
    }

    @MyLog("退出登录")
    @PostMapping("/logout")
    public R logout(HttpSession session){
        session.invalidate();
        return R.ok();
    }
}


